<?php


namespace app\applet\controller;


use app\BaseController;
use app\jiangnan\config\JNConstants;
use app\service\CarTypeService;
use app\service\cxyunService;
use app\utils\Constant;
use app\utils\JwtUtils;
use app\utils\StringUtils;
use think\facade\Db;

class CarMonthlyRental extends BaseController
{
    public function getList(){
        //return json(["code"=>0,'msg' => '获取成功',"parkingNo1"=>$this->request->parkingNo]);
        $accessToken=request()->header("accessToken");
        $userResult=JwtUtils::getDecodeToken($accessToken);
        //return json_encode($userResult);
        $userInfo=Db::table("userinfo")->where("id",$userResult["uid"])->find();
        if(empty($userInfo)){
            return json(["code"=>10030,'msg' => '用户未绑定房屋']);
        }
        $pklotStatus=false;
        $roominfo=Db::table("roominfo")->where("id",$userInfo["room_id"])->find();
        if(!empty($roominfo["pklot"]) ){
            $pklotStatus=true;
        }
        $carList=Db::table("car")
            ->where("room_id",$userResult["room_id"])
            ->where("carType",Constant::CAR_MONTHLY_RENTAL)
            ->select();
        foreach ($carList as &$value){
            $value["empMoblie"]=StringUtils::hidePhoneNumber($value["empMoblie"]);
        }

        //查询用户的车位号
        $pklotList=Db::table("cxyun_pklot")->where("empMoblie",$roominfo["phone"])->select();
        return json(["code"=>0,'msg' => '获取成功',"data"=>$carList,"pklotList"=>$pklotList,"roominfo"=>$roominfo,"pklotStatus"=>$pklotStatus]);
    }

    public function add_old(){
        $accessToken=request()->header("accessToken");
        $post=request()->post();

        $userResult=JwtUtils::getDecodeToken($accessToken);
        $userInfo=Db::table("userinfo")->where("id",$userResult["uid"])->find();
        if(empty($userInfo)){
            return json(["code"=>10030,'msg' => '用户未绑定房屋']);
        }

        $roominfo=Db::table("roominfo")->where("id",$userInfo["room_id"])->find();
        if(empty($roominfo) ){
            return json(["code"=>500,'msg' => '房屋信息错误']);
        }
        $data["parkingId"]="1699615639164002305";
        $data["carTypeId"]="1699615640275492865";
        $data["carModelId"]="1699615640116109313";
        $data["empName"]=$post["empName"];
        $data["empMoblie"]=$post["empMobile"];
        $data["plateNumber"]=$post["plateNumber"];
        $data["startTime"]="2024-01-01 00:00:00";
        $oneYearLater = strtotime("+3 year");
        $data["endTime"]="2027-01-01 00:00:00";
        $data["timestamp"]=time()."000";
        

        $array=Db::table("cxyun_parking_gate")
            ->where("parkingId",$data["parkingId"])
            ->where("isMonthlyStatus",1)
            ->select();
        $gateArr=array_column($array, 'gateId');
        $notData["gateId"]=implode(",",$gateArr);
        $notData["pklot"]=$roominfo["pklot"];
        $notData["familyAddress"]=$roominfo["community_name"].$roominfo["group"].$roominfo["building"]."-".$roominfo["unit"]."-".$roominfo["room"];
        //查询该该车场是否有该车辆
        //return json(["code"=>1,'msg' => '车牌添加成功',"data"=>$data]);
        $notData["parkingNo"]=$this->request->parkingNo;
        $car=Db::table("car")
            ->where("parkingId",$data["parkingId"])
            ->where("plateNumber",$data["plateNumber"])
            ->whereIn("carType",[1,3])
            ->find();
        if(!empty($car)){

            return json(["code"=>1,'msg' => '改车牌已经添加']);
        }
        //查询车场信息
        $parkinginfo=Db::table("parkinglotinfo")->where("parkingNo",$this->request->parkingNo)->find();
        if(empty($parkinginfo)){

            return json(["code"=>1,'msg' => '停车场信息错误']);
        }
        $cxyunApi=new cxyunService($parkinginfo['cxyun_appkey']);
        $res=$cxyunApi->commonPost("saveMonthlyRent",$data,$notData);
        $result= json_decode($res,true);
        if($result["status"]==1){
            $notData["update_time"]=date("Y-m-d H:i:s");
            unset($notData["gateId"]);
            $carInfo=array_merge($data,$notData);
            unset($carInfo["timestamp"]);
            $carInfo["room_id"]=$userInfo["room_id"];
            $carInfo["carType"]=Constant::CAR_MONTHLY_RENTAL;
            $carInfo["creator"]=$userInfo["id"];

            $re=Db::table("car")->insertGetId($carInfo);
            if($re){
                return json(["code"=>0,'msg' => '车牌添加成功']);
            }else{
                return json(["code"=>500,'msg' => "车牌添加失败,请重新添加"]);
            }

        }else{
            return json(["code"=>500,'msg' => $result["message"]]);
        }
    }

    public function add(){
        $accessToken=request()->header("accessToken");
        $post=request()->post();

        $userResult=JwtUtils::getDecodeToken($accessToken);
        $userInfo=Db::table("userinfo")->where("id",$userResult["uid"])->find();
        if(empty($userInfo)){
            return json(["code"=>10030,'msg' => '用户未绑定房屋']);
        }

        $roominfo=Db::table("roominfo")->where("id",$userInfo["room_id"])->find();
        if(empty($roominfo) ){
            return json(["code"=>500,'msg' => '房屋信息错误']);
        }

        $parkingInfo=Db::table("parkinglotinfo")
            ->where("parkingNo",$this->request->parkingNo)
            ->find();

        $carTypeInfo=(new CarTypeService())->getCarTypeInfo($this->request->parkingNo,1);
        if(empty($carTypeInfo)){
            return json(["code"=>500,'msg' => "添加失败，请联系管理员"]);
        }
        $data["parkingId"]=$carTypeInfo["parkingId"];
        $data["carTypeId"]=$carTypeInfo["carTypeId"];
        $data["carModelId"]=$carTypeInfo["carModelId"];
        $data["empName"]=$post["empName"];
        $data["empMoblie"]=$post["empMobile"];
        $data["plateNumber"]=$post["plateNumber"];
        $data["startTime"]="2024-01-01 00:00:00";
        $oneYearLater = strtotime("+3 year");
        $data["endTime"]="2027-01-01 00:00:00";
        $data["timestamp"]=time()."000";


        $array=Db::table("cxyun_parking_gate")
            ->where("parkingId",$data["parkingId"])
            ->where("isMonthlyStatus",1)
            ->select()->toArray();
        $gateArr=array_column($array, 'gateId');
        $notData["gateId"]=implode(",",$gateArr);
        $notData["pklot"]=$post["pklot"];
        $notData["familyAddress"]=$roominfo["community_name"].$roominfo["group"].$roominfo["building"]."-".$roominfo["unit"]."-".$roominfo["room"];
        //查询该该车场是否有该车辆
        //return json(["code"=>1,'msg' => '车牌添加成功',"data"=>$data]);
        $notData["parkingNo"]=$this->request->parkingNo;
        $car=Db::table("car")
            ->where("parkingId",$data["parkingId"])
            ->where("plateNumber",$data["plateNumber"])
            ->whereIn("carType",[1,3])
            ->find();
        if(!empty($car)){

            return json(["code"=>1,'msg' => '改车牌已经添加']);
        }
        $cxyunApi=new cxyunService($parkingInfo["cxyun_appkey"]);
        $res=$cxyunApi->commonPost("saveMonthlyRent",$data,$notData);
        $result= json_decode($res,true);
        if($result["status"]==1){
            $notData["update_time"]=date("Y-m-d H:i:s");
            unset($notData["gateId"]);
            $carInfo=array_merge($data,$notData);
            unset($carInfo["timestamp"]);
            $carInfo["room_id"]=$userInfo["room_id"];
            $carInfo["carType"]=Constant::CAR_MONTHLY_RENTAL;
            $carInfo["creator"]=$userInfo["id"];

            $re=Db::table("car")->insertGetId($carInfo);
            if($re){
                return json(["code"=>0,'msg' => '车牌添加成功']);
            }else{
                return json(["code"=>500,'msg' => "车牌添加失败,请重新添加"]);
            }

        }else{
            return json(["code"=>500,'msg' => $result["message"]]);
        }
    }
    public function delete(){
        $accessToken=request()->header("accessToken");
        $userResult=JwtUtils::getDecodeToken($accessToken);
        $userInfo=Db::table("userinfo")->where("id",$userResult["uid"])->find();
        if(empty($userInfo)){
            return json(["code"=>10030,'msg' => '用户未绑定房屋']);
        }
        $post=request()->post();
        if($userInfo["room_id"]!=$post["room_id"]){
            return json(["code"=>10040,'msg' => '无权操作']);
        }
        $parkingInfo=Db::table("parkinglotinfo")
            ->where("parkingNo",$this->request->parkingNo)
            ->find();
        $data["parkingId"]=$parkingInfo["cxyun_parking_id"];
        $data["plateNumber"]=$post["plateNumber"];
        $data["timestamp"]=time()."000";
        $notData=array();
        $cxyunApi=new cxyunService($parkingInfo["cxyun_appkey"]);
        $res=$cxyunApi->commonPost("delMonthlyRent",$data,$notData);
        $result= json_decode($res,true);
        if($result["status"]==1){
            $carInfo=Db::table("car")->where("id",$post["id"])->find();
            $re=Db::table("car")->where("id",$post["id"])->delete();
            $carInfo["delete_name"]=$userInfo["id"];
            $carInfo["delete_time"]=date("Y-m-d H:i:s");
            Db::table("car_delete")->insert($carInfo);
            if($re){
                return json(["code"=>0,'msg' => '车牌删除成功']);
            }else{
                return json(["code"=>500,'msg' => "车牌删除失败,请重新操作"]);
            }

        }else{
            return json(["code"=>500,'msg' => $result["message"]]);
        }
    }

    public function getReleaseList(){
        $accessToken=request()->header("accessToken");
        $userResult=JwtUtils::getDecodeToken($accessToken);
        $userInfo=Db::table("userinfo")->where("id",$userResult["uid"])->find();
        if(empty($userInfo)){
            return json(["code"=>10030,'msg' => '用户未绑定房屋']);
        }
        $pklotStatus=false;
        $roominfo=Db::table("roominfo")->where("id",$userInfo["room_id"])->find();
        if(!empty($roominfo["pklot"]) ){
            $pklotStatus=true;
        }
        $carList=Db::table("car")
            ->where("room_id",$userResult["room_id"])
            ->where("carType",3)
            ->select();
        foreach ($carList as &$value){
            $value["empMoblie"]=StringUtils::hidePhoneNumber($value["empMoblie"]);
        }
        return json(["code"=>0,'msg' => '获取成功',"data"=>$carList,"pklotStatus"=>$pklotStatus]);
    }

    public function addRelease(){
        $accessToken=request()->header("accessToken");
        $post=request()->post();

        $userResult=JwtUtils::getDecodeToken($accessToken);
        $userInfo=Db::table("userinfo")->where("id",$userResult["uid"])->find();
        if(empty($userInfo)){
            return json(["code"=>10030,'msg' => '用户未绑定房屋']);
        }

        $roominfo=Db::table("roominfo")->where("id",$userInfo["room_id"])->find();
        if(empty($roominfo) ){
            return json(["code"=>500,'msg' => '房屋信息错误']);
        }
        $count=Db::table("car")
            ->where("room_id",$userResult["room_id"])
            ->where("carType",3)
            ->count();
        if($count>0){
            return json(["code"=>500,'msg' => '地库放行最多添加一辆']);
        }

        $parkingInfo=Db::table("parkinglotinfo")
            ->where("parkingNo",$this->request->parkingNo)
            ->find();
        $carTypeInfo=(new CarTypeService())->getCarTypeInfo($this->request->parkingNo,1);
        if(empty($carTypeInfo)){
            return json(["code"=>500,'msg' => "添加失败，请联系管理员"]);
        }
        $data["parkingId"]=$carTypeInfo["parkingId"];
        $data["carTypeId"]=$carTypeInfo["carTypeId"];
        $data["carModelId"]=$carTypeInfo["carModelId"];

        $data["empName"]=$post["empName"];
        $data["empMoblie"]=$post["empMobile"];
        $data["plateNumber"]=$post["plateNumber"];
        $data["startTime"]="2024-01-01 00:00:00";
        $oneYearLater = strtotime("+3 year");
        $data["endTime"]="2027-01-01 00:00:00";
        $data["timestamp"]=time()."000";
        

        $array=Db::table("cxyun_parking_gate")
            ->where("parkingId",$data["parkingId"])
            ->where("isMonthlyStatus",1)
            ->select()->toArray();
        $gateArr=array_column($array, 'gateId');
        $notData["gateId"]=implode(",",$gateArr);
        $notData["pklot"]=$roominfo["pklot"];
        //$notData["familyAddress"]=$roominfo["community_name"].$roominfo["group"].$roominfo["building"]."-".$roominfo["unit"]."-".$roominfo["room"];
        $notData["familyAddress"]=$post["remarks"];
        //查询该该车场是否有该车辆
        //return json(["code"=>1,'msg' => '车牌添加成功',"data"=>$data]);
        $notData["parkingNo"]=$this->request->parkingNo;
        $car=Db::table("car")
            ->where("parkingId",$data["parkingId"])
            ->where("plateNumber",$data["plateNumber"])
            ->find();
        if(!empty($car)){
            if($car["carType"]==Constant::CAR_WELFARE){
                return json(["code"=>1,'msg' => '改车牌已经添加为福利车']);
            }
            return json(["code"=>1,'msg' => '改车牌已经添加']);
        }
        $cxyunApi=new cxyunService($parkingInfo["cxyun_appkey"]);
        $res=$cxyunApi->commonPost("saveMonthlyRent",$data,$notData);
        $result= json_decode($res,true);
        if($result["status"]==1){
            $notData["update_time"]=date("Y-m-d H:i:s");
            unset($notData["gateId"]);
            $carInfo=array_merge($data,$notData);
            unset($carInfo["timestamp"]);
            $carInfo["room_id"]=$userInfo["room_id"];
            $carInfo["carType"]=3;
            $carInfo["creator"]=$userInfo["id"];
            $carInfo["remarks"]=$post["remarks"];
            $re=Db::table("car")->insertGetId($carInfo);
            if($re){
                return json(["code"=>0,'msg' => '车牌添加成功']);
            }else{
                return json(["code"=>500,'msg' => "车牌添加失败,请重新添加"]);
            }

        }else{
            return json(["code"=>500,'msg' => $result["message"]]);
        }
    }

}